package net.factoryOpen.core.quickfactory.manufacturing.del;

import net.factoryOpen.core.common.codeManager.BillcodeManager;
import net.factoryOpen.core.common.status.BillStatus;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import net.factoryOpen.quickfactory.manufacturing.del.domain.DelDeliveryDeal;
import net.factoryOpen.quickfactory.manufacturing.del.domain.DelMps;
import net.factoryOpen.quickfactory.manufacturing.del.domain.DelOsplan;
import net.factoryOpen.quickfactory.manufacturing.del.domain.DelPlanItem;
import net.factoryOpen.quickfactory.manufacturing.del.mapper.DelDeliveryDealMapper;
import net.factoryOpen.quickfactory.manufacturing.del.mapper.DelMpsMapper;
import net.factoryOpen.quickfactory.manufacturing.del.mapper.DelOsplanMapper;
import net.factoryOpen.quickfactory.manufacturing.del.mapper.DelPlanItemMapper;
import net.factoryOpen.quickfactory.manufacturing.eng.domain.EngProductBasis;
import net.factoryOpen.quickfactory.manufacturing.eng.mapper.EngProductBasisMapper;
import net.factoryOpen.quickfactory.manufacturing.ord.domain.OrdSo;
import net.factoryOpen.quickfactory.manufacturing.ord.mapper.OrdContractItemMapper;
import net.factoryOpen.quickfactory.manufacturing.ord.mapper.OrdContractMapper;
import net.factoryOpen.quickfactory.manufacturing.ord.mapper.OrdSoItemMapper;
import net.factoryOpen.quickfactory.manufacturing.ord.mapper.OrdSoMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.Date;

/**
 *
 * @author factoryOpen
 * @date 2025-06-01
 * @version 1.0.0-draft.0
 */
@Service
public class DelDeliveryDealAlgorithm
{
    @Autowired
    private DelDeliveryDealMapper masterMapper;
    @Autowired
    private OrdSoMapper soMapper;
    @Autowired
    private EngProductBasisMapper productMapper;
    @Autowired
    private DelPlanItemMapper delplanMapper;
    @Autowired
    private DelOsplanMapper osplanMapper;
    @Autowired
    private DelMpsMapper mpsMapper;
    @Autowired(required = false)
    private OrdSoItemMapper soItemMapper;
    @Autowired(required = false)
    private OrdContractItemMapper contractItemMapper;
    @Autowired(required = false)
    private OrdContractMapper contractMapper;
    @Autowired
    private BillcodeManager billcodeManager;

    /**
     * 计划发货
     *
     * @param delDeliveryDeal 发货
     * @return 结果
     */
    public synchronized int planDelivery(DelDeliveryDeal delDeliveryDeal){
        Integer priority = (Integer) delDeliveryDeal.getParams().get("priority");
        Date promisday =delDeliveryDeal.getPromiseDay();

        double quantityOperating =0;
        Object obj = delDeliveryDeal.getParams().get("quantityOperating");
        if (obj.getClass().equals(String.class)){
            quantityOperating = Double.parseDouble((String)obj);
        }else if (obj.getClass().equals(Double.class)){
            quantityOperating = ((Double)obj).doubleValue();
        }else if (obj.getClass().equals(Integer.class)){
            quantityOperating = ((Integer)obj).doubleValue();
        }

        //读取订单明细
        Long objid = delDeliveryDeal.getId();
        DelDeliveryDeal masterObj = masterMapper.selectDelDeliveryDealById(objid);

        //取设发货计划号
        String billNo = billcodeManager.getNextBillno("DP");
        billcodeManager.updateBillSeq("DP");

        //创建发货计划项
        OrdSo soObj = soMapper.selectOrdSoById(masterObj.getSoId());
        DelPlanItem itemObj = new DelPlanItem();
        itemObj.setBillNo(billNo);
        itemObj.setSoId(masterObj.getSoId());
        itemObj.setSoItemId(masterObj.getId());
        itemObj.setStatus(BillStatus.BASE_ENABLE);
        itemObj.setProductId(masterObj.getProductId());
        itemObj.setProductCode(masterObj.getProductCode());
        itemObj.setProductName(masterObj.getProductName());
        itemObj.setProductSpec(masterObj.getProductSpec());
        itemObj.setDrawNo(masterObj.getDrawNo());
        itemObj.setCustomerProductNo(masterObj.getCustomerProductNo());
        itemObj.setUnitId(masterObj.getUnitId());
        itemObj.setQuantityPlanned(new BigDecimal(quantityOperating));
        itemObj.setPrice(masterObj.getPrice());
        itemObj.setDiscountPrice(masterObj.getDiscountPrice());
        itemObj.setDiscountRate(masterObj.getDiscountRate());
        itemObj.setNeedDay(masterObj.getNeedDay());
        itemObj.setPromiseDay(promisday);
        itemObj.setOrderDay(soObj.getOrderDay());
        itemObj.setPriority(priority);
        itemObj.setCustomerId(soObj.getCustomerId());
        itemObj.setProjectId(soObj.getProjectId());
        itemObj.setSalesId(soObj.getSalesId());
        itemObj.setAmount(new BigDecimal( quantityOperating * itemObj.getDiscountPrice().doubleValue()));
        itemObj.setFinalAmount(new BigDecimal(itemObj.getAmount().doubleValue() * itemObj.getDiscountRate().doubleValue()));
        //保存
        itemObj.refreshCreatingStamp();
        delplanMapper.insertDelPlanItem(itemObj);

        //更新订单明细的发货计划量、状态
        if (masterObj.getStatus()==BillStatus.BASE_ENABLE) masterObj.setStatus(BillStatus.ACTIVE_PLANNING);
        masterObj.setQuantityDelivering(new BigDecimal(masterObj.getQuantityDelivering().doubleValue() + quantityOperating));
        masterObj.refreshUpdatingStamp();
        masterMapper.updateDelDeliveryDeal(masterObj);

        //更新订单的状态
        if(soObj.getStatus()==BillStatus.BASE_ENABLE) {
            soObj.setStatus(BillStatus.ACTIVE_PLANNING);
            soObj.refreshUpdatingStamp();
            soMapper.updateOrdSo(soObj);
        }

        //更新产品的预约库存量
        EngProductBasis product = productMapper.selectEngProductBasisById(masterObj.getProductId());
        product.setQuantityBooked(new BigDecimal(product.getQuantityBooked().doubleValue() + quantityOperating));
        product.setUpdateBy(SecurityUtils.getUsername());
        product.setUpdateTime(DateUtils.getNowDate());
        productMapper.updateEngProductBasis(product);

        return 1;
    }

    /**
     * 计划转包
     *
     * @param delDeliveryDeal 转包
     * @return 结果
     */
    public synchronized int planOs(DelDeliveryDeal delDeliveryDeal){
        Integer priority = (Integer) delDeliveryDeal.getParams().get("priority");
        Date promisday =delDeliveryDeal.getPromiseDay();

        double quantityOperating =0;
        Object obj = delDeliveryDeal.getParams().get("quantityOperating");
        if (obj.getClass().equals(String.class)){
            quantityOperating = Double.parseDouble((String)obj);
        }else if (obj.getClass().equals(Double.class)){
            quantityOperating = ((Double)obj).doubleValue();
        }else if (obj.getClass().equals(Integer.class)){
            quantityOperating = ((Integer)obj).doubleValue();
        }

        //更新订单明细的转包计划量、状态
        Long objid = delDeliveryDeal.getId();
        DelDeliveryDeal masterObj = masterMapper.selectDelDeliveryDealById(objid);
        if (masterObj.getStatus()==BillStatus.BASE_ENABLE) masterObj.setStatus(BillStatus.ACTIVE_PLANNING);
        masterObj.setQuantitySubcontract(new BigDecimal(masterObj.getQuantitySubcontract().doubleValue() + quantityOperating));
        masterObj.refreshUpdatingStamp();
        masterMapper.updateDelDeliveryDeal(masterObj);

        //更新订单的状态
        OrdSo soObj = soMapper.selectOrdSoById(masterObj.getSoId());
        if(soObj.getStatus()==BillStatus.BASE_ENABLE) {
            soObj.setStatus(BillStatus.ACTIVE_PLANNING);
            soObj.refreshCreatingStamp();
            soMapper.updateOrdSo(soObj);
        }

        //取存单号
        String billNo = billcodeManager.getNextBillno("OS");
        billcodeManager.updateBillSeq("OS");
        //创建外协单，并设置特殊值
        DelOsplan osObj = new DelOsplan();
        osObj.setBillNo(billNo);
        osObj.setStatus(BillStatus.BASE_ENABLE);
        // 设置外协量
        osObj.setQuantityPlanned(new BigDecimal(quantityOperating));
        osObj.setQuantityDispatched(new BigDecimal(0));
        //从订单明细里复制字段值
        osObj.setSoId(masterObj.getSoId());
        osObj.setSoItemId(masterObj.getId());
        osObj.setProductId(masterObj.getProductId());
        osObj.setProductCode(masterObj.getProductCode());
        osObj.setProductName(masterObj.getProductName());
        osObj.setProductSpec(masterObj.getProductSpec());
        osObj.setDrawNo(masterObj.getDrawNo());
        osObj.setCustomerProductNo(masterObj.getCustomerProductNo());
        osObj.setUnitId(masterObj.getUnitId());
        osObj.setNeedDay(masterObj.getNeedDay());
        osObj.setPromiseDay(promisday);
        osObj.setOrderDay(soObj.getOrderDay());
        osObj.setPriority(priority);
        osObj.setCustomerId(soObj.getCustomerId());
        osObj.setProjectId(soObj.getProjectId());
        osObj.setSalesId(soObj.getSalesId());
        //保存外协计划
        osObj.refreshCreatingStamp();
        osplanMapper.insertDelOsplan(osObj);

        return 1;
    }

    /**
     * 计划自制
     *
     * @param delDeliveryDeal 投产
     * @return 结果
     */
    public synchronized int planMps(DelDeliveryDeal delDeliveryDeal){
        Integer priority = (Integer) delDeliveryDeal.getParams().get("priority");
        Date promisday =delDeliveryDeal.getPromiseDay();

        double quantityOperating =0;
        Object obj = delDeliveryDeal.getParams().get("quantityOperating");
        if (obj.getClass().equals(String.class)){
            quantityOperating = Double.parseDouble((String)obj);
        }else if (obj.getClass().equals(Double.class)){
            quantityOperating = ((Double)obj).doubleValue();
        }else if (obj.getClass().equals(Integer.class)){
            quantityOperating = ((Integer)obj).doubleValue();
        }

        //更新订单明细的自制计划量、状态
        Long objid = delDeliveryDeal.getId();
        DelDeliveryDeal masterObj = masterMapper.selectDelDeliveryDealById(objid);
        if (masterObj.getStatus()==BillStatus.BASE_ENABLE) masterObj.setStatus(BillStatus.ACTIVE_PLANNING);
        masterObj.setQuantityMps(new BigDecimal(masterObj.getQuantityMps().doubleValue() + quantityOperating));
        masterObj.refreshUpdatingStamp();
        masterMapper.updateDelDeliveryDeal(masterObj);

        //更新订单的状态
        OrdSo soObj = soMapper.selectOrdSoById(masterObj.getSoId());
        if(soObj.getStatus()==BillStatus.BASE_ENABLE) {
            soObj.setStatus(BillStatus.ACTIVE_PLANNING);
            soObj.refreshUpdatingStamp();
            soMapper.updateOrdSo(soObj);
        }

        //取存单号
        String billNo = billcodeManager.getNextBillno("MPS");
        billcodeManager.updateBillSeq("MPS");

        DelMps mpsObj = new DelMps();
        mpsObj.setBillNo(billNo);

        //判断类型
        if(soObj.getSoType().equals("0")) mpsObj.setMpsType(0);
        else if(soObj.getSoType().equals("1")) mpsObj.setMpsType(1);
        else if(soObj.getSoType().equals("2")) mpsObj.setMpsType(3);
        else if(soObj.getSoType().equals("3")) mpsObj.setMpsType(2);
        //状态设为已审
        mpsObj.setStatus(BillStatus.BASE_AUDIT_PASS);

        mpsObj.setSoId(masterObj.getSoId());
        mpsObj.setSoItemId(masterObj.getId());
        mpsObj.setProductId(masterObj.getProductId());
        mpsObj.setProductCode(masterObj.getProductCode());
        mpsObj.setProductName(masterObj.getProductName());
        mpsObj.setProductSpec(masterObj.getProductSpec());
        mpsObj.setDrawNo(masterObj.getDrawNo());
        mpsObj.setCustomerProductNo(masterObj.getCustomerProductNo());
        mpsObj.setUnitId(masterObj.getUnitId());
        mpsObj.setQuantityPlanned(new BigDecimal(quantityOperating));
        mpsObj.setNeedDay(masterObj.getNeedDay());
        mpsObj.setPromiseDay(promisday);
        mpsObj.setOrderDay(soObj.getOrderDay());
        mpsObj.setPriority(priority);
        mpsObj.setCustomerId(soObj.getCustomerId());
        mpsObj.setProjectId(soObj.getProjectId());
        mpsObj.setSalesId(soObj.getSalesId());
        //保存
        mpsObj.refreshCreatingStamp();
        mpsMapper.insertDelMps(mpsObj);

        return 1;
    }

}
